<script setup lang="ts">
import ViewContainer from "@/layouts/ViewContainer.vue";
import titleConfig from "./missing.title";
import Form from "@/components/StorageForm/index";
import { useScanStore, useToastStore } from "@/store";
import { missingService } from "@/service/missing";
import { useMessage } from "wot-design-uni";
import Spin from "@/components/Spin/index.vue";

const { missing } = useScanStore();
const message = useMessage();
const toastStore = useToastStore();

const Basic = Form.MissingBasicForm;
const Goods = Form.MissingGoods;

const basicInfo = reactive({
	mainOrderNumber: "",
	carNumber: null,
});

const loading = ref(false);

const currentId = ref();
const currentType = ref();
const currentMain = ref();

/* missing.setMain("172-85674245");
missing.setList([
	98654573434,
	98654573436,
	986545734378,
	98654573439,
	"UPPE240615502168",
]);
missing.setBox(22); */

// 初始化
const initView = async () => {
	loading.value = true;
	try {
		const rs = await missingService.getDetail(currentId.value);
		if (rs) {
			basicInfo.mainOrderNumber = rs.mainOrderNumber;
			missing.setMain(rs.mainOrderNumber);
			missing.setBox(rs.totalQuantity);
			missing.setList(rs.goodsLackDetailRespList?.map((e) => e.boxNumber));
		}
	} catch (error) {
		console.log(error);
	} finally {
		loading.value = false;
	}
};
const initViewByMainOrderNumber = () => {
	basicInfo.mainOrderNumber = currentMain.value;
	missing.setMain(currentMain.value);
	// handleSearch();
	return;
};

// 查询
const handleSearch = async ({ callback }) => {
	if (!basicInfo.mainOrderNumber) {
		// message.alert("主单号不可为空");
		toastStore.set("主单号不可为空", "error");
		return;
	}
	missing.setMain(basicInfo.mainOrderNumber);
	console.log(missing.getMain());
	
	callback?.();
	return;
	loading.value = true;
	try {
		const rs = await missingService.getBoxNumber(basicInfo.mainOrderNumber);
		const fligthRs = await missingService.getFlightInfo(
			basicInfo.mainOrderNumber,
		);

		if (rs && fligthRs) {
			missing.setMain(basicInfo.mainOrderNumber);
			missing.setList(rs);
			missing.setBox(fligthRs.boxQuantity);
			callback?.();
		}
	} catch (error) {
		console.log(error);
	} finally {
		loading.value = false;
	}
};

// 返回
const handleBack = () => {
	missing.clear();
	uni.navigateBack();
};

// 确认
const handleConfirm = async () => {
	if (!missing.getMain() && !basicInfo.mainOrderNumber) {
		// message.alert("主单号不可为空");
		toastStore.set("主单号不可为空", "error");

		return;
	}
	if (missing.getList().length === 0) {
		// message.alert("缺件货物信息不可为空");
		toastStore.set("缺件货物信息不可为空", "error");
		return;
	}
	loading.value = true;

	const _method =
		currentType.value === "edit"
			? missingService.update
			: missingService.create;
	try {
		const rs = await _method({
			id: currentId.value,
			mainOrderNumber: missing.getMain() ?? basicInfo.mainOrderNumber,
			lackQuantity: missing.getList().length,
			goodsLackDetailList: missing
				.getList()
				?.map((item) => ({ boxNumber: item })),
		});
		if (rs) {
			// message.alert(currentType.value === "edit" ? "编辑成功" : "登记缺件成功");
			toastStore.set(
				currentType.value === "edit" ? "编辑成功" : "登记缺件成功",
				"success",
			);
			setTimeout(() => {
				uni.reLaunch({ url: "/pages/index/index" });
				missing.clear();
			}, 1500);
		}
	} catch (error) {
		console.log(error);
	} finally {
		loading.value = false;
	}
};

onLoad((options) => {
	const { id, type, mainOrderNumber } = options;

	if ((!id || !type) && !mainOrderNumber) {
		return;
	}

	if (!mainOrderNumber) {
		currentId.value = id;
		currentType.value = type;
		initView();
		return;
	}
	currentMain.value = mainOrderNumber;
	initViewByMainOrderNumber();
});

onUnload(() => {
	missing.clear();
});
</script>

<template>
	<ViewContainer :title="titleConfig" innerClass="missing-register-page">
		<Spin :loading="loading" full>
			<Basic type="edit" v-model:data="basicInfo" showTitle @submit="handleSearch"
				:disabled="currentType === 'edit'" />
			<div class="divider"></div>
			<Goods showTitle :boxNumber="22" @scan="handleSearch" />
			<view class="btn-group">
				<wd-button @click="handleBack" class="btn back" type="info" :round="false">返回</wd-button>
				<wd-button @click="handleConfirm" class="btn confirm" :round="false">确认</wd-button>
			</view>
		</Spin>
	</ViewContainer>
</template>

<style lang="less" scoped>
.divider {
	width: 100%;
	height: 30px;
}

.btn-group {
	display: flex;
	justify-content: center;
	margin: 50px 0;

	.btn {
		width: 45%;
		height: 100px;
	}
}
</style>
